<%-- 
    Document   : register
    Created on : Nov 13, 2010, 1:34:18 PM
    Author     : edwinelpaisa
--%>

<%@page import="java.sql.SQLException"%>
<%@page import="java.sql.ResultSet"%>
<%
       model.ConnectionDB connection = new model.ConnectionDB();
       ResultSet res = null;
       String sql = "";
       String json = "";

        if(request.getParameter("action").equals("add")){
            sql = "SELECT * FROM AddUser('" + request.getParameter("id") + "','" + request.getParameter("mail") + "','" + request.getParameter("name") + "','" + request.getParameter("uname") + "','" + request.getParameter("telephone") +"','" + request.getParameter("password") +"',"+request.getParameter("type")+");";
            try {            
                connection.connect();
                connection.consult(sql);
                connection.getCon().close();
                if(connection.getRes().next())
                    out.print("{'errorcode' : " + connection.getRes().getInt(1) + "}"); //User Register Correctly
            } catch (SQLException e) {
                if(connection.getRes().next()){
                    out.print("{'errorcode' : " + connection.getRes().getInt(1) + "}");//User can't Register, duplicate
                }else{
                    out.print("{'errorcode' : -1}");//User can't Register
                    connection.getCon().close();
                }
            }
        }else if(request.getParameter("action").equals("listUsers")){
            sql = "SELECT * FROM GetUsers();";
            try {
                connection.connect();
                connection.consult(sql);
                res = connection.getRes();
                while(res.next()){
                    if (!json.equals("")){
                        json += ", ";
                    }
                    json += "{'user_id' : '" + res.getString(1) + "', ";
                    json += "'user_mail' : '" + res.getString(2) + "', ";
                    json += "'user_name' : '" + res.getString(3) + "', ";
                    json += "'user_uname' : '" + res.getString(4) + "', ";
                    json += "'user_telephone' : '" + res.getString(5) + "',";
                    json += "'user_type' : " + res.getInt(6) + "}";
                }
                out.print("{'errorcode' : 0, 'data' : [" + json + "]}");
                connection.getCon().close();
            } catch (SQLException e) {
                out.print("{'errorcode' : -1}");//Cant not get Users
                connection.getCon().close();
            }
        }else if(request.getParameter("action").equals("update")){
            if(request.getParameter("password") != null)
                sql = "SELECT UpdateUser('" + request.getParameter("id") + "','" + request.getParameter("mail") + "','" + request.getParameter("name") + "','" + request.getParameter("uname") + "','" + request.getParameter("telephone") +"','" + request.getParameter("password") +"',"+request.getParameter("type")+");";
            else
                sql = "SELECT UpdateUser('" + request.getParameter("id") + "','" + request.getParameter("mail") + "','" + request.getParameter("name") + "','" + request.getParameter("uname") + "','" + request.getParameter("telephone") +"',"+request.getParameter("type")+");";
            try {
                connection.connect();
                connection.consult(sql);
                connection.getCon().close();                
                out.print("{'errorcode' : 0}"); //User Updated Correctly
            } catch (SQLException e) {                
                out.print("{'errorcode' : -1}");//User can't Update
                connection.getCon().close();
            }
        }else if(request.getParameter("action").equals("delete")){
            sql = "SELECT DeleteUser('" + request.getParameter("user") + "');";

            try {
                connection.connect();
                connection.consult(sql);
                connection.getCon().close();
                out.print("{'errorcode' : 0}"); //User Deleted Correctly
            } catch (SQLException e) {
                out.print("{'errorcode' : -1}");//User can't Delete
                connection.getCon().close();
            }
        }else{
           out.print("{'errorcode' : -2}"); //Incorrect Access
        }
%>